package com.jivam.lottery.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import com.jivam.lottery.data.DataImport;
import com.jivam.lottery.util.Constants;

/**
 * 彩票主面板
 * @author Jivam
 */
public class Lottery extends JFrame implements ActionListener{
	private static final long serialVersionUID = 6453327693737649883L;
	JPasswordField pwd = new JPasswordField(7);
	JButton submit = new JButton("提交");
	JButton calculatePanel = new JButton(Constants.TOOL_CALCULATE);
	JButton analysePanel = new JButton(Constants.TOOL_ANALYSE);
	JButton openAll = new JButton("打开全部工具");
	JButton importData = new JButton("导入历史数据");
	
	public static boolean calculateFlag = false;
    public static boolean analyseFlag = false;
	
	private static Lottery lottery;
	public static Lottery getInstanse(){
		if(lottery == null){
			lottery = new Lottery();
		}
		return lottery;
	}
	
	private Lottery() {
		super(Constants.LOTTERY_TOOL+Constants.VERSION);
		init();//初始化图形
		addEventHandle();//初始化事件关联
	}

	private void init() {
		JPanel north = new JPanel();
		north.add(new JLabel("请输入密码:"));
		north.add(pwd);
		north.add(submit);
		
		JPanel center = new JPanel();
		calculatePanel.setEnabled(false);
		center.add(calculatePanel);
		analysePanel.setEnabled(false);
		center.add(analysePanel);
		importData.setEnabled(false);
		center.add(importData);
		openAll.setEnabled(false);
		center.add(openAll);
		
		this.add(north,BorderLayout.NORTH);
		this.add(center,BorderLayout.CENTER);
	}

	private void addEventHandle() {
		submit.addActionListener(this);
		calculatePanel.addActionListener(this);
		analysePanel.addActionListener(this);
		openAll.addActionListener(this);
		importData.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) {
		String command = ae.getActionCommand();
		if("提交".equals(command)){
			String pass = new String(pwd.getPassword());
			if("115699".equals(pass)){
				importData.setEnabled(true);
				calculatePanel.setEnabled(true);
				if(!DataImport.needImport()){
					analysePanel.setEnabled(true);
					openAll.setEnabled(true);
				}
				pwd.setEnabled(false);
				submit.setEnabled(false);
			}else{
				System.exit(0);
			}
		}else if("导入历史数据".equals(command)){
			if(DataImport.importFile()){
				analysePanel.setEnabled(true);
				openAll.setEnabled(true);
			}
			JOptionPane.showMessageDialog(this, DataImport.getImportMsg(), "导入提示", JOptionPane.INFORMATION_MESSAGE);
		}else{
			if(Constants.TOOL_CALCULATE.equals(command)){
				open(1);
			}else if(Constants.TOOL_ANALYSE.equals(command)){
				open(2);
			}else{
				open(3);
			}
			this.setVisible(false);
		}
		
	}
	
	private void open(int flag){
		if((flag & 0x01) == 1){
			Calculate.getInstanse().setVisible(true); 
			calculateFlag = true;
		}
        if((flag>>1 & 0x01) == 1){
        	Analyse.getInstanse().setVisible(true); 
			analyseFlag = true;
        }
	}
	
	public static void close(){
		if(!calculateFlag && !analyseFlag){
			getInstanse().setVisible(true); 
		}
	}
	
	public void showMe(){//show Method
		this.setSize(260,140);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//居中
		this.setLocation((screenSize.width-getWidth())/2,(screenSize.height-getHeight())/2);
		this.setAlwaysOnTop(true);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭按钮
	}

	public static void main(String[] args) {//Main Method
		getInstanse().showMe();
	}

	

}
